package com.banggood.upms.service.impl;

import com.banggood.silk.web.api.exception.SkbException;
import com.banggood.upms.bean.po.User;
import com.banggood.upms.bean.vo.LoginAttribute;
import com.banggood.upms.bean.vo.UpmsCredentials;
import com.banggood.upms.bean.vo.LoginResult;
import com.banggood.upms.common.constant.UpmsConstant;
import com.banggood.upms.common.util.AESHelper;
import com.banggood.upms.common.util.BasicAuthorizationUtil;
import com.banggood.upms.common.util.MessageDigest5Util;
import com.banggood.upms.dao.UserDao;
import com.banggood.upms.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author XuChuangFeng
 */
@Service
@Transactional(rollbackFor = SkbException.class)
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserDao userDao;

    @Value("${com.banggood.encrypt.aes}")
    private String aesKey;

    @Override
    public LoginResult login(HttpServletRequest request) throws Exception {
        // 解析authorization请求头，获取账号，原始密码跟请求IP
        UpmsCredentials upmsCredentials = BasicAuthorizationUtil.parseCredentialsFromRequest(request);
        // 查看这个账号的加密方式，OLD为老的OA加密方式，新添加的用户全部为NEW的加密方式
        User user = userDao.selectByAccount(upmsCredentials.getAccount());
        if(UpmsConstant.OLD_ENCRYPT_WAY.equalsIgnoreCase(user.getEncryptType())) {
            String encryptedPwd = AESHelper.encrypt(MessageDigest5Util.md5ToHex(upmsCredentials.getPassword()), aesKey);
            upmsCredentials.setPassword(encryptedPwd);
        } else {
            String encryptedPwd = MessageDigest5Util.md5ToHex(upmsCredentials.getPassword() + user.getEncryptSalt());
            upmsCredentials.setPassword(encryptedPwd);
        }
        // 调用登录接口，登录成功返回这个用户ID
        String userId = userDao.login(upmsCredentials);
        LoginResult result = new LoginResult();
        if(!StringUtils.isEmpty(userId)) {
            result.setAttribute(this.fillAttribute(user));
            result.setId(user.getAccountName());
            result.setSuccess(true);
            // 登录成功之后，更新该用户的登录信息
            userDao.updateLoginInfo(upmsCredentials);
        } else {
            result.setSuccess(false);
        }
        return result;
    }

    @Override
    public LoginResult updatePassword(HttpServletRequest request) throws Exception {
        return null;
    }

    private LoginAttribute fillAttribute(User user) {
        LoginAttribute attribute = new LoginAttribute();
        attribute.setHmsUserId(user.getHmsUserId());
        attribute.setOaId(user.getOaId());
        attribute.setEmail(user.getEmail());
        attribute.setPhone(user.getPhone());
        attribute.setRealName(user.getRealName());
        attribute.setUserId(user.getUserId());
        return attribute;
    }
}
